/**
 * @(#)com.framework.web.demo.repository.DemoRepository
 *
 * Copyright (c) 2014-2018 上海天跃科技有限公司
 *
 * DESC:
 *
 */
package com.framework.web.sys.repository;

import com.framework.web.sys.domain.Organization;
import com.framework.web.sys.domain.Resource;
import com.framework.web.sys.domain.Role;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

/**
 * 组织信息
 * @author fusf
 * @version 1.0  2016/1/7
 */
public interface OrganizationRepository extends PagingAndSortingRepository<Organization, String>,
        JpaSpecificationExecutor<Organization> {
        /**
         * 查询根节点
         * @return
         */
        @Query("select o from Organization o where o.parentId is null")
        public List<Organization> findRoot();

        /**
         * 查询子节点
          * @param parentId
         * @return
         */
         public List<Organization> findByParentIdOrderByLevel(String parentId);

        /**
         * 查询全部
         * @return
         */
        public List<Organization> findAll();

        /**
         * 获取第一层次最大level代码
         *
         * @return
         */
        @Query("select max(level) from Organization o where o.parentId = ?1")
        public String getMaxLevelByParentId(String parentId);

        /**
         * 获取第一层次最大level代码
         *
         * @return
         */
        @Query("select max(level) from Organization o where length(o.level) = 4")
        public String getMaxRootLevel();

        /**
         * 根据机构名称查询
         * @param orgName
         * @return
         */
        public List<Organization> findByOrgName(String orgName);

}
